package com.dinglian.ots.controller.order;

import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.OrderService;
import com.dinglian.ots.vo.order.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Api(value = "/order", tags = "订单模块")
@RequestMapping("/order/")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Value("${pic.url}")
    private String url;


    @ApiOperation("生成选款单")
    @PostMapping("createElectionOrder")
    public Result createElectionOrder(@RequestBody OrderVo orderVo) {
        try {
            orderService.createElectionOrder(orderVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("我的选款单")
    @GetMapping("myElectionOrder")
    public Result myElectionOrder(@RequestParam("userId") Integer userId,
                                  @RequestParam("seasonId") Integer seasonId,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        PageInfo<OrderInfoVo> orderInfoVos = new PageInfo<>();
        try {
            orderInfoVos = orderService.myElectionOrder(userId, seasonId, pageNum, pageSize);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(orderInfoVos, url);
    }


    @ApiOperation("我的选款单详情")
    @GetMapping("myElectionOrderInfo")
    public Result myElectionOrderInfo(@RequestParam("userId") Integer userId,
                                      @RequestParam("seasonId") Integer seasonId,
                                      @RequestParam("brandId") Integer brandId,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        List<ProductOrderVo> productOrderVos = new ArrayList<>();
        try {
            productOrderVos = orderService.myElectionOrderInfo(userId, seasonId, brandId, pageNum, pageSize);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(productOrderVos, url);
    }


    @ApiOperation("修改选款单")
    @PostMapping("updateMyElectionOrderInfo")
    public Result updateMyElectionOrderInfo(@RequestBody UpdateElectionOrder updateElectionOrder) {
        Map map = new HashMap();
        try {
            map = orderService.updateMyElectionOrderInfo(updateElectionOrder);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(map);
    }

    @ApiOperation("删除选款单")
    @PostMapping("deleteMyElectionOrderInfo")
    public Result deleteMyElectionOrderInfo(@RequestBody DeleteElectionOrder deleteElectionOrder) {
        Map map = new HashMap();
        try {
            map = orderService.deleteMyElectionOrderInfo(deleteElectionOrder);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(map);
    }


    @ApiOperation("导出选款单")
    @GetMapping(value = "exportElectionOrder", produces = "application/json; charset=UTF-8")
    public void exportElectionOrder(@RequestParam("userId") Integer userId, @RequestParam("seasonId") Integer seasonId, @RequestParam("brandId") Integer brandId, HttpServletResponse response) {
       /* UserVo userVo = (UserVo) SecurityUtils.getSubject().getSession().getAttribute(Constant.SESSION_USER);
        if (userVo != null){
            Integer userId1 = userVo.getUserId();
            if (!userId1.equals(userId)){
                throw new GlobalException(CodeMsg.DONT_TOUCH_ME);
            }
        }*/
        orderService.exportElectionOrder(userId, seasonId, brandId, response);
    }

    @ApiOperation("查询选款单政策")
    @GetMapping("getOrderListRule")
    public Result getOrderListRule(@RequestParam(value = "userId", required = true) Integer userId,
                                   @RequestParam(value = "brandId", required = true) Integer brandId) {
        Map ruleMap = new HashMap();
        try {
            ruleMap = orderService.getOrderListRule(userId,brandId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(ruleMap, url);
    }

    @ApiOperation("查询品牌选款单政策")
    @GetMapping("getBrandOrderListRule")
    public Result getBrandOrderListRule(@RequestParam(value = "userId", required = true) Integer userId,
                                        @RequestParam(value = "brandId", required = true) Integer brandId) {
        Map ruleMap = new HashMap();
        try {
            ruleMap = orderService.getBrandOrderListRule(userId,brandId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(ruleMap, url);
    }


}
